﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using OpenTK.Graphics.OpenGL;
using OpenTK;
using System.Drawing;
using System.Drawing.Imaging;

namespace SimpleModeler.Textures
{
    /// <summary>
    /// Stellt eine 2D-Textur dar
    /// </summary>
    public class Texture2D : Texture
    {
        #region Properties
        /// <summary>
        /// Pfad zur Datei für die Textur
        /// </summary>
        public string File { get; private set; }
        #endregion // Properties


        #region Constructors
        /// <summary>
        /// Konstruktor
        /// </summary>
        /// <param name="file">Pfad zur Datei für die Textur</param>
        public Texture2D(string file) : base() { if (System.IO.File.Exists(file)) this.File = file; }
        #endregion // Constructors


        #region Methods
        /// <summary>
        /// Lädt die Textur
        /// </summary>
        public override void LoadTexture()
        {
            if (this.Loaded == false && File != null)
            {
                this.TextureID = GL.GenTexture();
                GL.BindTexture(TextureTarget.Texture2D, this.TextureID);
                Bitmap TextureBitmap = new Bitmap(File);
                BitmapData data = TextureBitmap.LockBits(new System.Drawing.Rectangle(0, 0, TextureBitmap.Width, TextureBitmap.Height), ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
                GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba, data.Width, data.Height, 0, OpenTK.Graphics.OpenGL.PixelFormat.Bgra, PixelType.UnsignedByte, data.Scan0);
                TextureBitmap.UnlockBits(data);
                GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Linear);
                GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Linear);
                this.Loaded = true;
            }
        }
        #endregion // Methods
    }
}
